//
//  AppViewModel.swift
//  Exchange
//
//  Created by rui on 2018/6/15.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class AppViewModel: BaseViewModel {
    
    var dataArray = [TradeCoinModel]()
    var coinArray = [TradeCoinModel]()
    
    var updateInfo = [String:Any]()
    ///币种
    private lazy var tradeCoinApi: TradeCoinRequest = {
        let api = TradeCoinRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    ///版本更新
    private lazy var updateVersionApi: UpdateVersionRequest = {
        let api = UpdateVersionRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    private lazy var coinKindApi: CoinKindRequest = {
        let api = CoinKindRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    
    ///加载最新版本信息
    func loadUpdateVersion() {
        self.updateVersionApi.loadData()
    }
    
    func loadTradeCoin(resfresh: Bool = false) {
        if resfresh {
            self.tradeCoinApi.loadData()
        }else {
            EXFileManager.default.readAsync(form: EXFileManager.Paths.tradeCoinPath) { (data) in
                var flag = false
                if let jsonData = data {
                    do{
                        let models = try JSONDecoder().decode(Array<TradeCoinModel>.self, from: jsonData)
                        self.dataArray = models
                        flag = true
                    }catch {
                        flag = false
                        dPrint(error)
                    }
                }
                if flag {
                    self.response?(ResponseState.trade_coin,true, nil)
                }else {
                    self.tradeCoinApi.loadData()
                }
            }
        }
    }
    
    func loadCoin(resfresh: Bool = false) {
        if resfresh {
            self.coinKindApi.loadData()
        }else {
            EXFileManager.default.readAsync(form: EXFileManager.Paths.coinCoinPath) { (data) in
                var flag = false
                if let jsonData = data {
                    do{
                        let models = try JSONDecoder().decode(Array<TradeCoinModel>.self, from: jsonData)
                        self.coinArray = models
                        flag = true
                    }catch {
                        flag = false
                        dPrint(error)
                    }
                }
                if flag {
                    self.response?(ResponseState.coin_coin,true, nil)
                }else {
                    self.coinKindApi.loadData()
                }
            }
        }
    }
}

extension AppViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if let _ = request as? TradeCoinRequest {
            var flag = false
            if let data = data?["data"] as? [[String:Any]] {
                dPrint(data)
                if let jsonData = data.jsonData {
                    do{
                        
                        let models = try JSONDecoder().decode(Array<TradeCoinModel>.self, from: jsonData)
                        EXFileManager.default.writeAsync(data: jsonData, to: EXFileManager.Paths.tradeCoinPath) { (a) in
                            
                        }
                        self.dataArray = models
                        flag = true
                    }catch {
                        flag = false
                        dPrint(error)
                    }
                }
            }
            self.response?(ResponseState.trade_coin,flag, nil)
        }else if let _ = request as? CoinKindRequest {
            var flag = false
            if let data = data?["data"] as? [[String:Any]] {
                dPrint(data)
                if let jsonData = data.jsonData {
                    do{
                        
                        let models = try JSONDecoder().decode(Array<TradeCoinModel>.self, from: jsonData)
                        EXFileManager.default.writeAsync(data: jsonData, to: EXFileManager.Paths.coinCoinPath) { (a) in
                            
                        }
                        self.coinArray = models
                        flag = true
                    }catch {
                        flag = false
                        dPrint(error)
                    }
                }
            }
            self.response?(ResponseState.coin_coin,flag, nil)
        }else if let _ = request as? UpdateVersionRequest {
            if let data = data?["data"] as? [String:Any] {
                updateInfo = data
                self.response?(ResponseState.home_update_info,true, nil)
            }else {
                self.response?(ResponseState.home_update_info,false, nil)
            }
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if let _ = request as? TradeCoinRequest {
            self.response?(ResponseState.trade_coin
                ,false, request.errorMsg)
        }else if let _ = request as? CoinKindRequest {
            self.response?(ResponseState.coin_coin
                ,false, request.errorMsg)
            
        }else if let _ = request as? UpdateVersionRequest {
            self.response?(ResponseState.home_update_info,false, request.errorMsg)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? TradeCoinRequest {
            
        }else if let _ = request as? UpdateVersionRequest {
            if Bundle.main.infoDictionary != nil {
                if Bundle.main.infoDictionary!["CFBundleDisplayName"] != nil {
                    let projectName =   Bundle.main.infoDictionary!["CFBundleDisplayName"]!
                    pramas.updateValue(projectName, forKey: "name")
                }
            }
            pramas.updateValue("ios", forKey: "type")
        }
        return pramas
    }
    
}
